package com.hmall.search.mq;

import com.hmall.search.constants.MqConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class ItemListener {


    @Resource
    private MqSyncEsService mqSyncEsService;

    /**
     * 监听商品新增或修改的业务
     *
     * @param id 酒店id
     */
    @RabbitListener(queues = MqConstants.ITEM_INSERT_QUEUE)
    public void listenItemInsertOrUpdate(Long id) {
        // 此时 ES 在监听新增/修改队列，当该队列有消息时，会进行消费，消费的数据存储在 id 中
        // 根据id复查数据库，将查询后的数据，添加到 ES 中
        mqSyncEsService.insertById(id);
        log.info("listener mq insert or create => {}", id);
    }

    /**
     * 监听商品删除的业务
     *
     * @param id 酒店id
     */
    @RabbitListener(queues = MqConstants.ITEM_DELETE_QUEUE)
    public void listenItemDelete(Long id) {
        mqSyncEsService.deleteById(id);
        log.info("listener mq delete => {}", id);
    }
}
